// main.dart
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'file_picker.dart';
import 'file_saver.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '文件选择示例',
      home: Scaffold(
        appBar: AppBar(title: const Text('文件操作 (Web 兼容)')),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () async {
                  final bytes = await FilePickerHelper.pickFile();
                  if (bytes != null) {
                    print('读取到 ${bytes.length} 字节');
                    // 可以解析内容，比如文本
                    final text = String.fromCharCodes(bytes);
                    debugPrint(text.substring(0, 50));
                  } else {
                    print('用户取消选择');
                  }
                },
                child: const Text('选择文件'),
              ),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  // 示例：下载一个文本文件
                  const content = 'Hello, Flutter Web!\n这是下载的文件内容。';
                  final bytes = Uint8List.fromList(content.codeUnits);
                  FileSaver.saveFile(bytes, 'hello.txt');
                },
                child: const Text('下载文件'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}